package com.mzj.saas.mservice.roles.controller;

import com.mzj.saas.commons.ResultBean;
import com.mzj.saas.mservice.roles.service.RolesService;
import com.mzj.saas.mservice.roles.vo.RolePeopleVO;
import com.mzj.saas.mservice.roles.vo.RoleVO;
import com.mzj.saas.mservice.roles.vo.UserRoleRefVO;
import com.mzj.saas.mservice.sys.vo.ParamVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("roles")
public class RolesController {
    @Autowired
    private RolesService rolesService;

    /*
     *新增角色组
     */
    @PostMapping(value = "addRoleGroup")
    @ResponseBody
    public ResultBean<Object> addRoleGroup(@RequestHeader String accessToken, @RequestBody RoleVO roleVO) {
        return rolesService.addRoleGroup(accessToken, roleVO);
    }

    /*
     *新增角色
     */
    @PostMapping(value = "addRole")
    @ResponseBody
    public ResultBean<Object> addRole(@RequestHeader String accessToken, @RequestBody RoleVO roleVo) {
        return rolesService.addRole(accessToken, roleVo);
    }

    /*
     *修改角色组或角色名称
     */
    @PostMapping(value = "updateRoleName")
    @ResponseBody
    public ResultBean<Object> updateRoleName(@RequestHeader String accessToken, @RequestBody RoleVO roleVO) {
        return rolesService.updateRoleName(accessToken, roleVO);
    }

    /*
     *根据角色组Id显示角色列表
     */
    @PostMapping(value = "roleListByRoleGroup")
    @ResponseBody
    public ResultBean<Object> roleListByRoleGroup(@RequestHeader String accessToken, @RequestParam(name = "roleId", required = false) Long roleId) {
        return rolesService.roleListByRoleGroup(accessToken, roleId);
    }

    /*
     *更换主管理员
     */
    @PostMapping(value = "changeMainManager")
    @ResponseBody
    public ResultBean<Object> changeMainManager(@RequestHeader String accessToken,
                                                @RequestParam(name = "mainRoleId", required = false) Long mainRoleId,
                                                @RequestParam(name = "userId", required = false) String userId) {
        return rolesService.changeMainManager(accessToken, mainRoleId, userId);
    }

    /*
     *查询当前用户所具有权限的角色组
     */
    @GetMapping(value = "userRoleGroupList")
    @ResponseBody
    public ResultBean<Object> userRoleGroupList(@RequestHeader String accessToken, @RequestParam(name = "roleName", required = false) String roleName) {
        return rolesService.userRoleGroupList(accessToken, roleName);
    }

    /**
     * 角色下的用户成员
     *
     * @param accessToken
     * @param roleId
     * @return
     */
    @GetMapping(value = "rolePeopleList")
    @ResponseBody
    public ResultBean<List<RolePeopleVO>> rolePeopleList(@RequestHeader String accessToken,
                                                         @RequestParam(name = "roleId") Long roleId) {
        return rolesService.rolePeopleList(accessToken, roleId);
    }

    /*
     *新增角色下成员1
     */
    @PostMapping(value = "addRolePeople")
    @ResponseBody
    public ResultBean<Object> addRolePeople(@RequestHeader String accessToken,
                                            @RequestBody List<UserRoleRefVO> userRoleRefVOList) {
        return rolesService.addRolePeople(accessToken, userRoleRefVOList);
    }

    /*
     *移除角色下的成员1
     */
    @PostMapping(value = "deleteRolePeople")
    @ResponseBody
    public ResultBean<Object> deleteRolePeople(@RequestHeader String accessToken,
                                               @RequestBody List<UserRoleRefVO> userRoleRefVOList) {
        return rolesService.deleteRolePeople(accessToken, userRoleRefVOList);
    }

    /*
     *显示用户拥有权限的部门的所有成员
     */
    @PostMapping(value = "userPermissionDepartmentPeople")
    @ResponseBody
    public ResultBean<Object> userPermissionDepartmentPeople(@RequestHeader String accessToken,
                                                             @RequestParam(name = "roleId", required = false) Long roleId,
                                                             @RequestParam(name = "name", required = false) String name,
                                                             @RequestParam(name = "mobile", required = false) String mobile) {
        return rolesService.userPermissionDepartmentPeople(accessToken, roleId, name, mobile);
    }

    /**
     * 角色已添加人员
     *
     * @param accessToken
     * @param roleId
     * @return
     */
    @GetMapping(value = "roleAddedPeople")
    @ResponseBody
    public ResultBean<List<Map<String, Object>>> roleAddedPeople(@RequestHeader String accessToken,
                                                                 @RequestParam(name = "roleId") Long roleId) {
        return rolesService.roleAddedPeople(accessToken, roleId);
    }

    /*
     *修改角色权限
     */
    @PostMapping(value = "changeRolePermission")
    @ResponseBody
    public ResultBean<Object> changeRolePermission(@RequestHeader String accessToken, @RequestBody ParamVO paramVO) {
        return rolesService.changeRolePermission(accessToken, paramVO);
    }

    /*
     * 当前用户具有的角色
     */
    @PostMapping(value = "presentUserRoleGroupOtherRole")
    @ResponseBody
    public ResultBean<Object> presentUserRoleGroupOtherRole(@RequestHeader String accessToken) {
        return rolesService.presentUserRoleGroupOtherRole(accessToken);
    }

    /**
     * 删除角色
     *
     * @param accessToken
     * @param roleId
     * @return
     */
    @GetMapping("/deleteRole")
    @ResponseBody
    public ResultBean<Object> deleteRole(@RequestHeader String accessToken,
                                         @RequestParam(name = "roleId") Long roleId) {
        return rolesService.deleteRole(accessToken, roleId);
    }

    /**
     * 删除角色组
     *
     * @param accessToken
     * @param roleGroupId
     * @return
     */
    @GetMapping("/deleteRoleGroup")
    @ResponseBody
    public ResultBean<Object> deleteRoleGroup(@RequestHeader String accessToken,
                                              @RequestParam(name = "roleGroupId") Long roleGroupId) {
        return rolesService.deleteRoleGroup(accessToken, roleGroupId);
    }


}
